package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
)

func main()  {
	// file write
	file, e := os.OpenFile("filewrite.txt", os.O_CREATE|os.O_WRONLY, 0666)
	if e != nil {
		fmt.Println("文件打开失败")
		return
	} else {
		fmt.Println("文件打开成功")
	}
	// 挂起关闭程序
	defer file.Close()

	pwd, _ := os.Getwd()
	fmt.Println(pwd)
	filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
		//fmt.Println("dayinwenjianlujing",path)
		//fmt.Println(info.Name())
		fileHandle(file,path)
		return nil
	})

}

// the file handler ,read the file
func fileHandle(file *os.File,path string) (err error){
	if strings.HasSuffix(path,".go") {
		fmt.Println(path)
		file.WriteString(path)

		f, err := os.Open(path)
		if err != nil {
			fmt.Println(err)
		}
		defer f.Close()

		fileRead := bufio.NewReader(f)
		for {
			line, err := fileRead.ReadBytes('\n')
			if err != nil {
				if err == io.EOF {
					return nil
				}
				return err
			}
			//fmt.Println(string(line))
			lineHandler(file,string(line))

		}
	}
	return nil
}

// lines handler,write file
func lineHandler(file *os.File,line string)  {
	if strings.HasPrefix(line,"func") || strings.HasPrefix(line,"type")  {
		fmt.Println(line)
		//file.WriteString(strings.TrimSuffix(line,"{"))
		file.WriteString(strings.TrimRight(line,"{"))
	}
}

// line write the file
func lineWrite()  {
	file, e := os.OpenFile("d:/kaka.txt", os.O_CREATE|os.O_WRONLY, 0666)
	if e != nil {
		fmt.Println("文件打开失败")
		return
	} else {
		fmt.Println("文件打开成功")
	}
	// 挂起关闭程序
	defer file.Close()
}


